home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.dcs.warwick.ac.uk!not-for-mail
- From: D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero)
- Subject: problems with a linked list
- X-Nntp-Posting-Host: flat
- Message-ID: <1996Jan25.125329.23499@dcs.warwick.ac.uk>
- Sender: news@dcs.warwick.ac.uk (Network News)
- Organization: Department of Computer Science, Warwick University, England
- Date: Thu, 25 Jan 1996 12:53:29 GMT
-
-
- Dear c community
- I'm having some problems with a small c program which uses pointers
- and would like to know if any of you could give me a hand.
- The following program is intended to create a linked list
- of three integers, inserting each one at the beginning of
- the current list, and then print the three integers,
- but when I run it I get a segmentation fault.
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
-
- struct side
- {
- int a;
- struct side *next;
- };
-
- struct side *i;
- struct side *first_side;
- struct side *tmp;
-
- /* insert a new element in the list */
- void store_side(struct side *i, struct side *first_side) {
- if (first_side == NULL) { first_side = i; first_side->next = NULL; }
- else { i->next = first_side; first_side = i; }
- }
-
- void main(void) {
-
- /* insert first element */
- first_side = NULL;
- i = malloc(1*sizeof(struct side));
- (i->a) = 0;
- store_side(i, first_side);
-
- /* insert second element */
- i = malloc(1*sizeof(struct side));
- (i->a) = 1;
- store_side(i, first_side);
-
- /* insert third elemenet */
- i = malloc(1*sizeof(struct side));
- (i->a) = 2;
- store_side(i, first_side);
-
- /* print the three elements */
- tmp = first_side;
- printf("side: %d \n",tmp->a);
- while (tmp->next != NULL) {
- tmp = tmp->next;
- printf("side: %d \n",tmp->a);
- }
-
- }
-
- --------------------------------------------------
-
- Any help would be greatly appreciated.
- --
- * Daniel Castillo. D.C.Molero@dcs.warwick.ac.uk *
-
-
-